package com.wx.xg.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wx.xg.pojo.AdminUser;
import com.wx.xg.pojo.User;
import com.wx.xg.service.AdminUserService;
import com.wx.xg.service.UserService;
import com.wx.xg.utils.MD5;
import com.wx.xg.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 54
 * @since 2021-05-29
 */

@RestController
@Api(tags = "用户登录")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AdminUserService adminUserService;

    @RequestMapping(path = "/login/{userName}/{passWord}", method = RequestMethod.GET)
    @ApiOperation(value = "用户登录检测")
    public R userLogin(@PathVariable("userName") String userName, @PathVariable("passWord") String passWord) {

        String userNameNull = "用户名错误！";
        String passWordNull = "密码错误！";
        if (userName == null) {
            return R.error().data("userName", userNameNull);
        }
        if (passWord == null) {
            return R.error().data("passWord", passWordNull);
        }
        QueryWrapper<User> selectQuery = new QueryWrapper();
        QueryWrapper<AdminUser> AdminSelectQuery = new QueryWrapper();
        selectQuery.eq("username", userName);
        selectQuery.eq("password", passWord);
        int count = userService.count(selectQuery);
        if (count > 0) {
            List<User> userList = userService.list();
            for (User user : userList) {
                user.setPassword(MD5.encrypt(user.getPassword()));
            }
            return R.ok().data("user",userList);
        }
        selectQuery.eq("a_username", userName);
        selectQuery.eq("a_password", passWord);
        count = adminUserService.count(AdminSelectQuery);
        if (count > 0) {
            List<AdminUser> adminList = adminUserService.list();
            for (AdminUser adminUser : adminList) {
                adminUser.setAPassword(MD5.encrypt(adminUser.getAPassword()));
            }
            return R.ok().data("admin",adminList);
        }
        return R.error();
    }


}

